<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="static/easyui/jquery.min.js"></script>
<script type="text/javascript" src="static/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="static/easyui/locale/easyui-lang-zh_CN.js"></script>
<link rel="stylesheet" href="static/easyui/themes/metro/easyui.css"/>
<link rel="stylesheet" href="static/easyui/themes/icon.css"/>
<title>药房请领</title>
</head>
<body>
<script type="text/javascript">
  $(function(){
	  //药房药物初始化
 	 $('#drugRoomDetails-tab').datagrid({
		    url:'selectAll-drugs',
		    pagination:true,
		    pageNumber:1,
		    pageSize:5,
		    pageList:[4,5],
		    columns:[[
		    	{field:'drug_number',title:'药品编号',width:120,checkbox:true},
	    		{field:'type_name',title:'药品类型',width:147},
				{field:'drug_name',title:'药品名称',width:167},
				{field:'drug_retail_price',title:'药品零售价',width:127},
				{field:'drug_standard',title:'药品规格',width:147},
				{field:'drug_unit',title:'单位',width:119},
				{field:'inventory_details_stardate',title:'生产日期',width:166,formatter:comFormatDate2},
				{field:'inventory_details_usedate',title:'有效期',width:113}
		    ]]
		});
	  
 	 //药物查询
 	$("#search-drugname").searchbox({
			searcher:function(value){
				//访问数据源、初始化表格
				 $('#drugRoomDetails-tab').datagrid({
				    url:'selectAll-drugs',
				    queryParams:{
				    	drugname:value 
				    },
				    pagination:true,
				    pageNumber:1,
				    pageSize:5,
				    pageList:[4,5],
				    columns:[[
			    	    {field:'drug_number',title:'药品编号',width:120,checkbox:true},
			    		{field:'type_name',title:'药品类型',width:147},
						{field:'drug_name',title:'药品名称',width:167},
						{field:'drug_retail_price',title:'药品零售价',width:127},
						{field:'drug_standard',title:'药品规格',width:147},
						{field:'drug_unit',title:'单位',width:119},
						{field:'inventory_details_stardate',title:'生产日期',width:166,formatter:comFormatDate2},
						{field:'inventory_details_usedate',title:'有效期',width:113}
			       ]]
				});
			}
 	    });	
 	 
	 //药物追加的按钮
	$("#drugRoomDetails-addsubmits").click(function(){
		//获取表格中的信息
		var rows =$("#drugRoomDetails-tab").datagrid('getSelections');
		var copyRows = [];
        for (var j= 0; j < rows.length; j++) {
            copyRows.push(rows[j]); //将数据添加到copyRows数组中     
        }
        for(var i =0;i<copyRows.length;i++){ 
			var falg = true;
			var index = $('#drugRoomDetails-tab').datagrid('getRowIndex',copyRows[i]);
			//获取追加的表格的行
			var recordrows = $("#YesdrugRoomDetails-tab").datagrid("getRows");
			for(var m=0;m<recordrows.length;m++){
			     //判断该datagrid在将要添加的数据中是否存在
			     if(copyRows[i].drug_number == recordrows[m].drug_number){					      
			        	falg = false;
			     }
			} 
             if(falg){
            	 $("#YesdrugRoomDetails-tab").datagrid('appendRow',{
            		 drug_number:rows[i].drug_number,
            		 type_name:rows[i].type_name,
            		 drug_name:rows[i].drug_name,
            		 drug_retail_price:rows[i].drug_retail_price,
            		 drug_standard:rows[i].drug_standard,
            		 drug_unit:rows[i].drug_unit,
            		 inventory_details_stardate:rows[i].inventory_details_stardate,
            		 inventory_details_usedate:rows[i].inventory_details_usedate
 		         });
             }
        }
        //清除所有所选的项
        $("#drugRoomDetails-tab").datagrid('clearChecked');	
	});
	 
	 //药物追加表格
	 $('#YesdrugRoomDetails-tab').datagrid({
		    url:'',
		    pagination:true,
		    pageNumber:1,
		    pageSize:3,
		    pageList:[3,2],
		    columns:[[
			    	    {field:'drug_number',title:'药品编号',hidden:true},
			    		{field:'type_name',title:'药品类型',width:120},
						{field:'drug_name',title:'药品名称',width:140},
						{field:'drug_retail_price',title:'药品零售价',width:90},
						{field:'drug_standard',title:'药品规格',width:140},
						{field:'drug_unit',title:'单位',width:97},
						{field:'inventory_details_stardate',title:'生产日期',width:150,formatter:comFormatDate2},
						{field:'inventory_details_usedate',title:'有效期',width:125},
						{field:'drug_shuliang',title:'数量',width:76,align:'center',
 							formatter:function(value,row,index){
								 //输入框
								  var sliang="<input  class='drugamounts' type='number' min='1' style='width:60px;' value='1'/>"
								 return sliang;
							}	
 						},
 						{field:'drug_removes',title:'操作',width:75,align:'center',
 							formatter:function(value,row,index){
 								 return  "<a onclick='drug_removes("+row.drug_number+")'>移除</a>"
								
							}	
 						}
		    ]]
		});
	 
	 
	 
	 //请领提交
	 $("#YesdrugRoomDetails-addsubmits").click(function(){
		
   			$("<form method='post' id='forYesdrug'>").appendTo("body").form({
   				url:'save-drugInStorage',
				onSubmit:function(param){
					//获取处理状态---药库还未处理
					param["drugapply.drugapplyState"] = 1;
					
					//请领类型---门诊还是住院
					param["drugapply.drugapplyType"] = 1;
					
					//请领人
					param["drugapply.drugapplyPeople"] =$("#applypeople").val();
					console.log(param["drugapply.drugapplyPeople"]);
					
					//获取表格中的入库的记录
					var rows = $("#YesdrugRoomDetails-tab").datagrid("getRows");
				    var total=0;
					for(var  i = 0;i < rows.length; i++){
						//药品总数量
						total +=parseInt($(".drugamounts:eq(' "+i+" ')").val()); 
					    console.log("药品总数量："+total  );
					    //药品编号
					    param["DrugapplyDetail["+i+"].drug.drugNumber"] = rows[i]["drug_number"];
					    //console.log("药品编号："+ param["DrugapplyDetail["+i+"].drug.drugNumber"] );
						//单个药品请领领药量
						param["DrugapplyDetail["+i+"].dpDetailsShuliang"] = $(".drugamounts:eq(' "+i+" ')").val();
						//console.log("单个药品请领领药量："+param["DrugapplyDetail["+i+"].dpDetailsShuliang"]  );
					}
					
					//单个药品请领数量
					param["DrugapplyDetail.dpDetailsShijishuliang"]=0;
					
					//总数量	
					param["drugapply.drugapplyAllnumber"] = parseInt(total);
   				  
   				},
   				success:function(data){
   					if(parseInt(data)==1){
   						$(".window-apply").text("你的请领成功！！！");
   						$("#applysaveclinic-win").window('open');
				    	//三秒之后自动消失
					   setTimeout(function(){
						   $("#applysaveclinic-win").window('close');
						  }, 3000);
				    	
					 $('#forYesdrug').form("clear");//清空文本
					//清空选中的药品
					$("#drugRoomDetails-tab").datagrid('clearChecked');
					$("#YesdrugRoomDetails-tab").datagrid("loadData", {total:0, rows:[]});
   					}
   					
   					if(parseInt(data)==2){
  					     $(".window-apply").text("添加失败,稍后重试......");
				    	 $("#applysaveclinic-win").window('open');
				    	//三秒之后自动消失
					   setTimeout(function(){
					    	 $("#applysaveclinic-win").window('close');
						  }, 3000);
				    	
					  $('#forYesdrug').form("clear");//清空文本
					  //清空选中的药品
					  $("#drugRoomDetails-tab").datagrid('clearChecked');
					  $("#YesdrugRoomDetails-tab").datagrid("loadData", {total:0, rows:[]});
  					}
   					
   				}
   			}).submit();
	  });
 	        	
  });
	 
	//药物移除表格一行
    function drug_removes(drugnumber){
    	 var row = $('#YesdrugRoomDetails-tab').datagrid('getSelected');
    	 if (row) {
             var rowIndex = $('#YesdrugRoomDetails-tab').datagrid('getRowIndex', row);
             $('#YesdrugRoomDetails-tab').datagrid('deleteRow', rowIndex);
         }
     }
  
	//格式化时间
	function comFormatDate(date) {
		var day = new Date(date); 
		var Year = 0; 
		var Month = 0; 
		var Day = 0; 
		var CurrentDate = ""; 
		//初始化时间 
		Year= day.getFullYear();//ie火狐下都可以 
		Month= day.getMonth()+1; 
		Day = day.getDate(); 
		Hour = day.getHours(); 
		 Minute = day.getMinutes(); 
		 Second = day.getSeconds(); 
		CurrentDate += Year + "-"; 
		if (Month >= 10 ) { 
			CurrentDate += Month + "-"; 
		}else { 
			CurrentDate += "0" + Month + "-"; 
		} 
		if (Day >= 10 ) { 
			CurrentDate += Day ; 
		}else { 
			CurrentDate += "0" + Day ; 
		} 
		if(Hour>=10){
			CurrentDate +=" "+Hour;
		}else{
			CurrentDate +=" "+"0"+Hour;
		}
		if(Minute>=10){
			CurrentDate +=":"+Minute;
		}else{
			CurrentDate +=":"+"0"+Minute;
		}
		if(Second>=10){
			CurrentDate +=":"+Second;
		}else{
			CurrentDate +=":"+"0"+Second;
		}
		return CurrentDate; 
	}
	
	//格式化时间
	function comFormatDate2(date) {
		var day = new Date(date); 
		var Year = 0; 
		var Month = 0; 
		var Day = 0; 
		var CurrentDate = ""; 
		//初始化时间 
		Year= day.getFullYear();//ie火狐下都可以 
		Month= day.getMonth()+1; 
		Day = day.getDate(); 
		CurrentDate += Year + "-"; 
		if (Month >= 10 ) { 
			CurrentDate += Month + "-"; 
		}else { 
			CurrentDate += "0" + Month + "-"; 
		} 
		if (Day >= 10 ) { 
			CurrentDate += Day ; 
		}else { 
			CurrentDate += "0" + Day ; 
		} 
		
		return CurrentDate; 
	}
</script>	
<!-- 面板	 -->
  <fieldset style="border:1px solid #A6A6A6;border-radius:6px;width:1050px;overflow: hidden;margin:0 auto;margin-top:10px;">
    <legend style="font-size:17px">药品信息</legend>
        <a style="margin-left:14px;">
			<input id="search-drugname" name="search-checkdrugname" 
			class="easyui-searchbox"  data-options="width:160,height:30,prompt:'请输入药物名称或者类型'">
		  </a>			       
        <div style="margin-left:15px;margin-top:8px;">
	     	<table id="drugRoomDetails-tab" class="easyui-datagrid" data-options="fitColumns:false,checkOnSelect:false,width:1016,height:185" >
		  
		    </table>
		     <p style="width:103px;margin:0 auto;margin-left: 88.5%;display:inline-block;margin-top: 8px;">
    		          <a  id="drugRoomDetails-addsubmits" class="easyui-linkbutton"   
    		          data-options="iconCls:'icon-ok',border:false">添加至请领单</a> 
            </p>
            
		    <!-- 药物选择完之后的盒子 -->
		    <div style="margin-top:8px;">
		        <table id="YesdrugRoomDetails-tab" class="easyui-datagrid" data-options="fitColumns:false,singleSelect:true,width:1016,height:142" >
		  
		       </table>
		        <p style="width:100px;margin:0 auto;margin-left:93%;display:inline-block;margin-top: 8px;">
    		          <a  id="YesdrugRoomDetails-addsubmits" class="easyui-linkbutton"   data-options="iconCls:'icon-ok',border:false">确定</a> 
                </p>
		    </div>
	</div>	<!-- tab盒子 -->
									       
</fieldset>

 <input type="hidden" value="${user.userName}"  id="applypeople">
 <!-- 弹框提示 -->
<div id="applysaveclinic-win" class="easyui-window"  title="提示框" style="width:300px;height:156px"
    data-options="iconCls:'icon-save',modal:true,resizable:false,collapsible:false,maximizable:false,minimizable:false,closed:true">
          <p class="window-apply" style=" margin-left: 72px;font-size: 16px;margin-top: 47px;">
          </p>
</div>

</body>
</html>


